<template>
  <div>
    <section class="choose-link" @click="showLink = true">
      <div class="content" v-if="!linkName">
        <i class="iconfont icon-lianjie"></i>
        <p>选择链接</p>
      </div>
      <div class="link" v-else>
        <a-input disabled :value="linkName" />
      </div>
    </section>

    <!-- <div v-for="(el, key) in link"> -->
    <template v-if="params && params.length">
      <div class="flex" v-for="item of params">
        <em>{{ item.key }}:</em>
        <a-input v-model="item.value" @change="changeVal" :placeholder="item.desc" />
      </div>
    </template>
    <!-- </div> -->

    <!-- <select-link
      v-if="showLink"
      @onOk="onOk"
      @cancel="showLink = false"
    ></select-link> -->

    <new-select-link v-if="showLink" @onOk="onOk" @cancel="showLink = false"></new-select-link>

    <!-- v-if="showLink" -->
  </div>
</template>

<script>
import SelectLink from '@/components/DiyNew/SelectLink/index.vue'
import newSelectLink from '@/components/DiyNew/newSelectLink/index.vue'
import { getDiyURLText } from '@/utils/utils'
export default {
  name: '',
  props: {
    link: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  components: {
    SelectLink,
    newSelectLink,
  },
  data() {
    return {
      showLink: false,
      hasLink: false,
      params: [],
    }
  },

  mounted() {
    console.log('链接参数： ', this.hasLink)
  },
  computed: {
    linkName() {
      return this.link.link || ''
    },
  },

  watch: {
    link: {
      handler(newVal) {
        if (newVal.params) {
          this.$nextTick(() => {
            this.params = newVal.params
          })
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    onOk(e) {
      this.$emit('chooseEnd', e)
      this.showLink = false
    },

    changeVal() {
      let link = JSON.parse(JSON.stringify(this.link))
      link.params = this.params
      link = getDiyURLText(link)
      this.$emit('chooseEnd', link)
    },
  },
}
</script>

<style lang="scss" scoped>
.choose-link {
  width: 228px;
  height: 40px;
  line-height: 40px;
  background: #f6f7f9;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  section {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .content {
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 40px;
    text-align: center;
    width: 100%;
    overflow: hidden;

    .iconfont {
      font-size: 24px;
    }
    p {
      margin: 0 0 0 4px;
      font-size: 14px;
      font-weight: 600;
      color: #000000;
    }
  }
  .link {
    font-size: 14px;
    font-weight: 600;
    color: #000000;
  }
}

.flex {
  display: flex;
  align-items: center;
  overflow: hidden;
  margin-top: 10px;

  em {
    margin-right: 10px;
    font-style: normal;
  }
}

::v-deep {
  .ant-input-disabled {
    background-color: #f6f7f9;
    color: rgba(0, 0, 0, 0.65);
    cursor: pointer;
  }
}
</style>
